﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class ProductManager : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            get_All_Category();
            get_All_Product();            
        }        
    }

    //public int countProduct = 1;
    public void get_All_Product()
    {        
        cpPaging.PageSize = 30;
        cpPaging.DataSource = BusLogic.Product.get_ALL_PRODUCT().Tables[0].DefaultView;
        cpPaging.BindToControl = ProductRepeater;
        ProductRepeater.DataSource = cpPaging.DataSourcePaged;
        ProductRepeater.DataBind();
    }
    

    protected void cbxCheckALLProduct_CheckedChanged(object sender, EventArgs e)
    {
        if (!cbxCheckALLProduct.Checked)
        {
            foreach (RepeaterItem item in ProductRepeater.Items)
            {
                CheckBox cb = (CheckBox)item.FindControl("cbxCheckProduct");
                cb.Checked = false;

            }
        }
        else
        {
            foreach (RepeaterItem item in ProductRepeater.Items)
            {
                CheckBox cb = (CheckBox)item.FindControl("cbxCheckProduct");
                cb.Checked = true;
            }
        }
    }

    protected void lbtDelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in ProductRepeater.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("cbxCheckProduct");

            if (cb.Checked == true)
            {
                TextBox txtProductId = (TextBox)item.FindControl("txtProductId");
                BusLogic.Product.remove_PRODUCT(int.Parse(txtProductId.Text));
            }
        }
        get_All_Product();
        cbxCheckALLProduct.Checked = false;
    }

    public void get_All_Category()
    {
        ddlCategoryName.DataSource = BusLogic.Category.get_ALL_CATEGORY();
        ddlCategoryName.DataValueField = "ID";
        ddlCategoryName.DataTextField = "NAME";
        ddlCategoryName.DataBind();

        ListItem item = new ListItem();
        item.Value = "0";
        item.Text = "Danh mục sản phẩm";
        item.Selected = true;
        ddlCategoryName.Items.Add(item);
        
        ddlCategoryName.SelectedValue = "0";
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        string sCatId = ddlCategoryName.SelectedValue;
        ProductRepeater.DataSource = BusLogic.Product.search_PRODUCT(txtProductName.Text, int.Parse(sCatId));
        ProductRepeater.DataBind();
    }
    protected void btReset_Click(object sender, EventArgs e)
    {          
        Response.Redirect("ProductManager.aspx");
    }

    public string get_Image_Product(object data)
    {
        string sImageUrl = data.ToString();

        String[] sArrImage = sImageUrl.Split(new Char[] { ';' });

        return sArrImage[0];
    } 
}
